-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test median #3
Test median #3
Conversation
} else { | ||
Arrays.sort(numbers); | ||
int median; | ||
if (numbers.length % 2 == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variables can be extracted from 'if':
int size = numbers.length;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
*/ | ||
|
||
import java.util.Arrays; | ||
|
||
public class Median { | ||
public int median(int[] numbers){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommended to use descriptive verbs that indicate what the method does
Here are some examples: findMedian
, getMedian
, calculateMedian
It can also return a double value if the length is even-numbered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I used assertEquals for double was error, that I use deprecated method, so first i tried with int. Now I found solution.
*/ | ||
|
||
import java.util.Arrays; | ||
|
||
public class Median { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this file to a named package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if (numbers == null) { | ||
return Integer.MAX_VALUE; | ||
} | ||
if (numbers.length == 0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There no spaces between ){
It's better to format the code: Ctrl+Alt+L
int median; | ||
if (numbers.length % 2 == 0) { | ||
int middleIndex = numbers.length / 2; | ||
median = (numbers[middleIndex] + numbers[middleIndex - 1]) /2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the length is even, the code returns an incorrect result
Probably because of the int cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
No description provided.